package com.dbn.webmagic.pageprocessor;

import com.dbn.webmagic.dao.*;
import com.dbn.webmagic.entity.*;
import com.dbn.webmagic.utils.UrlDetail;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author liheping
 * @Description //农业农村部科技发展中心数据处理
 * @Date 2021/11/29 16:42
 **/
@Component
public class NoticePipeline implements Pipeline {

    @Autowired
    private AgentDao agentDao;
    @Autowired
    private ApplyAdressDao applyAdressDao;
    @Autowired
    private ApplyNameDao applyNameDao;
    @Autowired
    private VarChangeDao varChangeDao;
    @Autowired
    private VarietyDao varietyDao;
    @Autowired
    private VarNameChangeDao varNameChangeDao;
    @Autowired
    private WorkItemChangeDao workItemChangeDao;
    @Autowired
    private ComnoticeDao comnoticeDao;
    @Autowired
    private NoticeDao noticeDao;

    @Override
    @Transactional
    public void process(ResultItems resultItems, Task task) {
        String url = resultItems.getRequest().getUrl();
        if(url.contains(UrlDetail.comnotice_URL)){
            String noticesimpleid=resultItems.get("noticesimpleid");
            String applyno=resultItems.get("applyno");
            Document document=resultItems.get("comdocument");
            List<Agent> agentList = new ArrayList<>();
            List<Variety> varietyList = new ArrayList<>();
            List<VarChange> varChangeList = new ArrayList<>();
            List<WorkItemChange> workItemChangeList = new ArrayList<>();
            List<ApplyAdress> applyAdresList = new ArrayList<>();
            List<ApplyName> applyNameList = new ArrayList<>();
            List<VarNameChange> varNameChangeList = new ArrayList<>();
            Comnotice comnotice=new Comnotice();
            Agent agent=new Agent();
            Variety variety=new Variety();
            VarChange varChange=new VarChange();
            WorkItemChange workItemChange=new WorkItemChange();
            ApplyAdress applyAdres =new ApplyAdress();
            ApplyName applyName=new ApplyName();
            VarNameChange varNameChange=new VarNameChange();
            Elements elements=document.getElementById("Content_tbInfo").getElementsByTag("tr");
            List<Map<String, String>> list=new ArrayList<>();
            for (Element element : elements) {
                Map<String,String> map=new HashMap<>();
                String rightname=element.getElementsByTag("td").get(1).text();
                String leftname=element.getElementsByTag("td").get(0).text();
                map.put(leftname,rightname);
                list.add(map);
            }
            for (int i=0;i<list.size();i++) {
                String key=list.get(i).keySet().toString();
                String keyvalue=key.substring(1,key.length()-1);
                if(keyvalue.equals("植物种类")){
                    comnotice.setPlanttype(list.get(i).get(keyvalue));
                }if(keyvalue.equals("品种名称")){
                    comnotice.setTempname(list.get(i).get(keyvalue));
                }if(keyvalue.equals("申请日")){
                    comnotice.setApplydate(list.get(i).get(keyvalue));
                }if(keyvalue.equals("申请人")){
                    comnotice.setApplyname(list.get(i).get(keyvalue));
                }if(keyvalue.equals("目前状态")){
                    comnotice.setStatus(list.get(i).get(keyvalue));
                }if(keyvalue.equals("申请公告日")){
                    comnotice.setApplynodedate(list.get(i).get(keyvalue));
                }if(keyvalue.equals("共同申请人")){
                    comnotice.setTogetherapply(list.get(i).get(keyvalue));
                }if(keyvalue.equals("共同品种权人")){
                    comnotice.setTogethervariet(list.get(i).get(keyvalue));
                }if(keyvalue.equals("申请的撤回日")){
                    comnotice.setApplybackdate(list.get(i).get(keyvalue));
                }if(keyvalue.equals("品种权终止日")){
                    comnotice.setVarietystopdate(list.get(i).get(keyvalue));
                }if(keyvalue.equals("品种权人")){
                    comnotice.setVariety(list.get(i).get(keyvalue));
                }if(keyvalue.equals("视为撤回日")){
                    comnotice.setRecalldate(list.get(i).get(keyvalue));
                }if(keyvalue.equals("视为放弃日")){
                    comnotice.setGiveupdate(list.get(i).get(keyvalue));
                }if(keyvalue.equals("权利的恢复日")){
                    comnotice.setVarback(list.get(i).get(keyvalue));
                }if(keyvalue.equals("代理人变更日")){
                    String before=list.get(i+1).values().toString();
                    String after=list.get(i+2).values().toString();
                    String before1=before.substring(1,before.length()-1);
                    String after1=after.substring(1,after.length()-1);
                    agent.setActchangedate(list.get(i).get(keyvalue));
                    agent.setBefchange(before1);
                    agent.setAftchange(after1);
                    agent.setApplyno(applyno);
                    agentList.add(agent);
                    i=i+2;
                }if(keyvalue.equals("品种权的转让日")||keyvalue.equals("品种权转让日")){
                    String before=list.get(i+1).values().toString();
                    String after=list.get(i+2).values().toString();
                    String before1=before.substring(1,before.length()-1);
                    String after1=after.substring(1,after.length()-1);
                    variety.setAftervariety(list.get(i).get(keyvalue));
                    variety.setBeforownname(before1);
                    variety.setAfterownname(after1);
                    variety.setApplyno(applyno);
                    varietyList.add(variety);
                    i=i+2;
                }if(keyvalue.equals("著录项目变更日")){
                    String before=list.get(i+1).values().toString();
                    String after=list.get(i+2).values().toString();
                    String before1=before.substring(1,before.length()-1);
                    String after1=after.substring(1,after.length()-1);
                    workItemChange.setWorkitemchangedate(list.get(i).get(keyvalue));
                    workItemChange.setBefworkitemchange(before1);
                    workItemChange.setAftworkitemchange(after1);
                    workItemChange.setApplyno(applyno);
                    workItemChangeList.add(workItemChange);
                    i=i+2;
                }
                if(keyvalue.equals("品种权人变更日")){
                    String before=list.get(i+1).values().toString();
                    String after=list.get(i+2).values().toString();
                    String before1=before.substring(1,before.length()-1);
                    String after1=after.substring(1,after.length()-1);
                    varChange.setVarchangedate(list.get(i).get(keyvalue));
                    varChange.setBeforvarchangename(before1);
                    varChange.setAftervarchangename(after1);
                    varChange.setApplyno(applyno);
                    varChangeList.add(varChange);
                    i=i+2;
                }if(keyvalue.equals("申请人地址变更日")){
                    String before=list.get(i+1).values().toString();
                    String after=list.get(i+2).values().toString();
                    String before1=before.substring(1,before.length()-1);
                    String after1=after.substring(1,after.length()-1);
                    applyAdres.setApplyadressdate(list.get(i).get(keyvalue));
                    applyAdres.setBefapplyadress(before1);
                    applyAdres.setAftapplyadress(after1);
                    applyAdres.setApplyno(applyno);
                    applyAdresList.add(applyAdres);
                    i=i+2;
                }if(keyvalue.equals("申请人变更日")){
                    String before=list.get(i+1).values().toString();
                    String after=list.get(i+2).values().toString();
                    String before1=before.substring(1,before.length()-1);
                    String after1=after.substring(1,after.length()-1);
                    applyName.setApplynamedate(list.get(i).get(keyvalue));
                    applyName.setBefapplyname(before1);
                    applyName.setAftapplyname(after1);
                    applyName.setApplyno(applyno);
                    applyNameList.add(applyName);
                    i=i+2;
                }if(keyvalue.equals("品种名称变更日")){
                    String before=list.get(i+1).values().toString();
                    String after=list.get(i+2).values().toString();
                    String before1=before.substring(1,before.length()-1);
                    String after1=after.substring(1,after.length()-1);
                    varNameChange.setVarnamechangedate(list.get(i).get(keyvalue));
                    varNameChange.setBefvarnamechang(before1);
                    varNameChange.setAftvarnamechang(after1);
                    varNameChange.setApplyno(applyno);
                    varNameChangeList.add(varNameChange);
                    i=i+2;
                }
            }
            comnotice.setApplyno(applyno);
            comnotice.setNoticesimpleid(noticesimpleid);
            comnoticeDao.insert(comnotice);
            if(agentList.size()!=0)
                agentDao.insertList(agentList);
            if(varietyList.size()!=0)
                varietyDao.insertList(varietyList);
            if(varChangeList.size()!=0)
                varChangeDao.insertList(varChangeList);
            if(workItemChangeList.size()!=0)
                workItemChangeDao.insertList(workItemChangeList);
            if(applyAdresList.size()!=0)
                applyAdressDao.insertList(applyAdresList);
            if(applyNameList.size()!=0)
                applyNameDao.insertList(applyNameList);
            if(varNameChangeList.size()!=0)
                varNameChangeDao.insertList(varNameChangeList);
        }
        if(url.contains(UrlDetail.notice_URL)){
            String noticesimpleid=resultItems.get("noticesimpleid");
            String applyno=resultItems.get("applyno");
            Document document=resultItems.get("noticedocument");
            NoticeEntity notice =new NoticeEntity();
            Elements elements=document.getElementById("Content_tbInfo").getElementsByTag("tr");
            String noticetitle=document.getElementById("Content_lblTitle").text();//公告标题
            String phase=document.getElementById("Content_lblSubTitle").text();//公告期刊
            notice.setNoticetitle(noticetitle);
            notice.setPhase(phase);
            notice.setApplyno(applyno);
            notice.setNoticesimpleid(noticesimpleid);
            for (Element element : elements) {
                String s=element.getElementsByTag("td").get(0).text();
                String leftname=s.replace("\u00A0", "");//去空格
                String rightname=element.getElementsByTag("td").get(1).text();
                if(leftname.equals("植物种类")){
                    notice.setPlanttype(rightname);
                }if(leftname.equals("品种暂定名称")){
                    notice.setZstempname(rightname);
                }if(leftname.equals("品种名称")){
                    notice.setTempname(rightname);
                }if(leftname.equals("申请日")){
                    notice.setApplydate(rightname);
                }if(leftname.equals("申请人")){
                    notice.setApplyname(rightname);
                }if(leftname.equals("公告日")){
                    notice.setNodedate(rightname);
                }if(leftname.equals("公告号")){
                    notice.setNodeno(rightname);
                }if(leftname.equals("培育人")){
                    notice.setFostername(rightname);
                }if(leftname.equals("申请人地址")){
                    notice.setApplymanadd(rightname);
                }if(leftname.equals("申请日前销售情况")){
                    notice.setApplysell(rightname);
                }if(leftname.equals("类型")){
                    notice.setNoticetype(rightname);
                }if(leftname.equals("原因")){
                    notice.setReason(rightname);
                }if(leftname.equals("品种权人")){
                    notice.setVariety(rightname);
                }if(leftname.equals("品种权人地址")){
                    notice.setVarietyadd(rightname);
                }if(leftname.equals("代理机构")){
                    notice.setAgency(rightname);
                }if(leftname.equals("代理机构地址")){
                    notice.setAgencyadd(rightname);
                }if(leftname.equals("代理人")){
                    notice.setAgencyman(rightname);
                }if(leftname.equals("品种权证书号")){
                    notice.setVarietycartno(rightname);
                }if(leftname.equals("变更后")){
                    notice.setAfterchange(rightname);
                }if(leftname.equals("变更前")){
                    notice.setBeforchange(rightname);
                }if(leftname.equals("转让前")){
                    notice.setBefortran(rightname);
                }if(leftname.equals("转让后")){
                    notice.setAftertran(rightname);
                }if(leftname.equals("终止日")){
                    notice.setStopdate(rightname);
                }
            }
            noticeDao.insert(notice);
        }
    }
}
